#include "gpio.h"
//#include "adc.h"

#include "stdio.h"

#include "fun.h"

#include "led.h"
#include "lcd.h"
#include "key.h"
#include "i2c_hal.h"


extern uint16_t count;
char text[20];
uint16_t lcd_highshow =0;

extern float cnbr_para;
extern float vnbr_para;
int cnbr_data;
int vnbr_data;
int idle_data=8;
extern int page;

extern uint16_t B1_state;
extern uint16_t B1_last_state ;
extern uint16_t B2_state;
extern uint16_t B2_last_state ;
extern uint16_t B3_state;
extern uint16_t B3_last_state ;
extern uint16_t B4_state;
extern uint16_t B4_last_state ;

extern int flag;
int clear=0;


//------------------------------------------------------------------------

void lcdshow_para_or_data(void)
{
		int a;
		a=HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_7);
		sprintf(text ," PA7:%d",a);
		LCD_DisplayStringLine(Line0, (u8 *)text);	
	
		if(	B1_state==0  &&  B1_last_state== 1)
		{}
		else if(B1_state==0  &&  B1_last_state== 0)
		{}
		else if(B1_state==1  &&  B1_last_state== 0)
		{
//				page++;
//				page=page%2;	
		}
		
		if(page == 0)
					lcd_show_Data();
		else if(page == 1)
					lcd_show_Para();
}

void lcd_show_Para(void)
{			
			if(clear !=1)
			{
					clear =1;
					LCD_Clear(Black);	
			}
			sprintf(text ,"       Para ");
			LCD_DisplayStringLine(Line2, (u8 *)text);
			sprintf(text ,"   CNBR:%.2f",cnbr_para);
			LCD_DisplayStringLine(Line4, (u8 *)text);	
			sprintf(text ,"   VNBR:%.2f",vnbr_para);
			LCD_DisplayStringLine(Line6, (u8 *)text);
}
void lcd_show_Data(void)
{
			if(clear !=0)
			{
					clear =0;
					LCD_Clear(Black);	
			}
			sprintf(text ,"       Data ");
			LCD_DisplayStringLine(Line2, (u8 *)text);
			sprintf(text ,"   CNBR:%d",cnbr_data);
			LCD_DisplayStringLine(Line4, (u8 *)text);	
			sprintf(text ,"   VNBR:%d",vnbr_data);
			LCD_DisplayStringLine(Line6, (u8 *)text);
			sprintf(text ,"   IDLE:%d",idle_data);
			LCD_DisplayStringLine(Line8, (u8 *)text);
}




//--------------------------------------------------------------

void eeprom_write(uint8_t addr,uint8_t dat)
{
		I2CStart();
	
		I2CSendByte(0xa0);
		I2CWaitAck();
		I2CSendByte(addr);
		I2CWaitAck();
		I2CSendByte(dat);
		I2CWaitAck();
	
		I2CStop();
	
		HAL_Delay (20);
}

uint8_t eeprom_read(uint8_t addr)
{
		I2CStart();
		I2CSendByte(0xa0);
		I2CWaitAck();
		I2CSendByte(addr);
		I2CWaitAck();
		I2CStop();
	
		I2CStart();
		I2CSendByte(0xa1);
		I2CWaitAck();
		uint8_t dat = I2CReceiveByte();
		I2CSendNotAck();
		I2CStop();
	
		return dat;
}

	
	
	
